package com.xmu.edu.tisp.contract.landimplementation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.xmu.edu.tisp.R;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ImplementationDeleteListAdapter extends BaseAdapter {
	private Context context;
	private List<HashMap<String, String>> list;
	int All = 0;
	int isAll = 0;
	HashMap<String, String> checked = new HashMap<String, String>();
	CheckBox allCheck;
	
	public ImplementationDeleteListAdapter (Context context , List<HashMap<String, String>> list , int isAll , CheckBox allcheck){
		super();
		this.context = context;
		this.list = list;
		this.isAll = isAll;
		allCheck = allcheck;
		if(isAll==1){
			for(int i=0 ; i<list.size();++i){
				checked.put(i+"",list.get(i).get("XJSZ_ZYSQQR_DETAIL_ID") );
			}
		}
		All=list.size();
		
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int index = position;
		View view = convertView;
		if(view == null){
			LayoutInflater inflater = (LayoutInflater) context
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        view = inflater.inflate(R.layout.implementation_checkbox_listview_item, null);
		}
		
		HashMap<String, String> map = list.get(position);
		TextView field_cd = (TextView) view.findViewById(R.id.ICLI_field_cd);
		field_cd.setText(map.get("FIELD_BLOCK_CODE"));
		
		TextView field_name = (TextView) view.findViewById(R.id.ICLI_field_name);
		field_name.setText("(" + map.get("FIELD_MANAGER") + ")");
		
		EditText edit = (EditText) view.findViewById(R.id.ICLI_zs_edittext);
		edit.setText(map.get("ZS"));
		
		
		CheckBox checkbox = (CheckBox) view.findViewById(R.id.ICLI_checkBox);
		checkbox.setOnCheckedChangeListener(new CheckedListener(position));
		if (checked.containsKey(position + "")) {
			checkbox.setChecked(true);
		} else {
			checkbox.setChecked(false);
		}
		if (map.get("FIELD_BLOCK_CODE").equals("")) {
			checkbox.setVisibility(IGNORE_ITEM_VIEW_TYPE);

		}
		
		return view;
	}
	
	public ArrayList<String> getChecked() {
		ArrayList<String> XJSZ_ZYSQQR_ID_LIST = new ArrayList<String>();
		Iterator<String> it = checked.keySet().iterator();
		while (it.hasNext()) {
			String s = it.next();

			XJSZ_ZYSQQR_ID_LIST.add(checked.get(s));
		}
		return XJSZ_ZYSQQR_ID_LIST;
	}
	
	class CheckedListener implements OnCheckedChangeListener {
		int position;

		public CheckedListener(int position) {
			this.position = position;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (isChecked == true) {
				checked.put(position+"",list.get(position).get("XJSZ_ZYSQQR_DETAIL_ID") );
				if(checked.size()==All){
					allCheck.setClickable(false);
					allCheck.setChecked(true);
					allCheck.setClickable(true);
				}
				else{
					{allCheck.setClickable(false);
				    allCheck.setChecked(false);
				     allCheck.setClickable(true);}
				}
			} else {
				allCheck.setClickable(false);
				allCheck.setChecked(false);
				allCheck.setClickable(true);
				if (checked.containsKey(position + "")) {
					checked.remove(position + "");
					
					}

				}
			}

		}

}
